package com.housediyadmin.view
{
	/**
	 * 
	 * @author Bo Zhang
	 * 2011/01/07
	 */	
	import com.adobe.cairngorm.process.ProcessEvent;
	import com.housediyadmin.events.bubbleEvents.CheckSalesIDBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.DeleteSalesBubbleEvt;
	import com.housediyadmin.events.bubbleEvents.EditSalesBubbleEvt;
	import com.housediyadmin.events.goodsEvents.GetGoodsBySalesIDEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByTypeIDEvent;
	import com.housediyadmin.events.salesEvents.AddSalesEvent;
	import com.housediyadmin.events.salesEvents.DeleteSalesEvent;
	import com.housediyadmin.events.salesEvents.EditSalesEvent;
	import com.housediyadmin.events.salesEvents.GetSalesEvent;
	import com.housediyadmin.events.treeEvents.DeleteAreaLevelEvent;
	import com.housediyadmin.events.treeEvents.GetGoodsTreeEvent;
	import com.housediyadmin.model.AreaTreeModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.model.HouseModel;
	import com.housediyadmin.model.SalesModel;
	import com.housediyadmin.service.SalesRemoteObjectService;
	import com.housediyadmin.view.components.SalesPopUp;
	import com.housediyadmin.view.components.EditTreeView;
	import com.housediyadmin.view.components.TreeView;
	import com.housediyadmin.vo.Goods;
	import com.housediyadmin.vo.House;
	import com.housediyadmin.vo.Sales;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.controls.dataGridClasses.DataGridColumn;
	import mx.core.IFlexDisplayObject;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;
	import mx.events.IndexChangedEvent;
	import mx.events.ListEvent;
	import mx.events.Request;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.List;
	import spark.events.IndexChangeEvent;
	
	public class SalesAdminMediator extends Mediator
	{
		[Inject]
		public var view:SalesAdminView;
		
		[Inject]
		public var goodsTreeModel:GoodsTreeModel
		
		[Inject]
		public var salesModel:SalesModel;
		
		[Inject]
		public var sales:SalesRemoteObjectService;
		
		[Inject]
		public var goodsModel:GoodsModel;
		
		override public function onRegister():void
		{
			// sales.TimeTest1();	
			BindingUtils.bindProperty(view.salesList, "dataProvider", salesModel, "salesArray");	
			dispatch(new GetSalesEvent(GetSalesEvent.GET_ALL_SALES, null));		
			var salesIds:ArrayCollection = new ArrayCollection();			
			salesIds.addItem(salesModel.salesIds);		
			// sales相关
			view.addSalesBtn.addEventListener(MouseEvent.CLICK, popUpAddSalesView);
			view.deleteSeveralSalesBtn.addEventListener(MouseEvent.CLICK, deleteSeveralSales);		

			view.addEventListener(EditSalesBubbleEvt.EDIT_A_SALES, popUpEditSalesView);
			view.addEventListener(DeleteSalesBubbleEvt.DELETE_A_SALES, deleteEve);
			view.addEventListener(CheckSalesIDBubbleEvt.ADD_TO_SALESIDS, addIdToSalesIdsHandler);
		}
		
		private function popUpEditSalesView(event:EditSalesBubbleEvt):void
		{
			salesModel.selectedSales = event.sales;
			var popUpEditView:SalesPopUp = new SalesPopUp();
			popUpEditView.currentState = "edit";
			PopUpManager.addPopUp(popUpEditView, contextView);
			PopUpManager.centerPopUp(popUpEditView);
			mediatorMap.createMediator(popUpEditView);
			popUpEditView.addEventListener(CloseEvent.CLOSE, close);	
		}
		
		private function popUpAddSalesView(event:MouseEvent):void
		{
			salesModel.selectedSales = new Sales(); 
			var popUpEditView:SalesPopUp = new SalesPopUp();
			popUpEditView.currentState = "add";
			PopUpManager.addPopUp(popUpEditView, contextView);
			PopUpManager.centerPopUp(popUpEditView);
			mediatorMap.createMediator(popUpEditView);
			popUpEditView.addEventListener(CloseEvent.CLOSE, close);
		}
		
		public function close(event:CloseEvent):void
		{
			event.target.removeEventListener(CloseEvent.CLOSE, close);
			PopUpManager.removePopUp(event.target as IFlexDisplayObject);
			mediatorMap.removeMediatorByView(event.target as IFlexDisplayObject);
		}
		
		private function addIdToSalesIdsHandler(event:CheckSalesIDBubbleEvt):void
		{
			if (salesModel.salesIds.contains(event.salesId))
			{
				var index:int = salesModel.salesIds.getItemIndex(event.salesId);
				salesModel.salesIds.removeItemAt(index);
			}
			else
			{
				salesModel.salesIds.addItem(event.salesId);
			}
		}
			
		private function deleteEve(event:DeleteSalesBubbleEvt):void
		{
			Alert.yesLabel = "是";
			Alert.noLabel = "否";
			Alert.cancelLabel = "取消";
			Alert.show("删除相应的促销信息吗？", "", 3, view, deleteAlert);
			salesModel.salesIds = new ArrayCollection();
			salesModel.salesIds.addItem(event.salesId);
		}
		
		private function deleteAlert(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var salesArr:Array = new Array;
				salesArr.push(salesModel.salesIds[0]);
				dispatch(new DeleteSalesEvent(DeleteSalesEvent.DELETE_SALES, salesArr));			
			}
		}
		
		private function deleteSeveralSales(event:MouseEvent):void
		{
			if (salesModel.salesIds.length == 0)
			{
				Alert.show("请选择删除的促销");
			}
			else
			{
				Alert.yesLabel = "是";
				Alert.noLabel = "否";
				Alert.cancelLabel = "取消";
				Alert.show("删除相应的促销吗？", "", 3,view, deleteSalesHandler);			
			}	
		}
		
		private function deleteSalesHandler(event:CloseEvent):void
		{
			if(event.detail == Alert.YES)
			{
				var salesArr:Array = new Array;
				var i:int = 0
				for (i; i < salesModel.salesIds.length; i++)
				{
					salesArr.push(salesModel.salesIds[i]);
				}
				dispatch(new DeleteSalesEvent(DeleteSalesEvent.DELETE_SALES, salesArr));
			}	
		}
		
		private function selectTreeNode(event:ListEvent):void
		{
			goodsTreeModel.selectedNode = event.target.selectedItem as XML;
			
			var goodsIDs:Array = new Array;
			var xmlList:XMLList = event.target.selectedItem.@typeID;
			
			if(xmlList.length()!=0)
			{
				for each(var xml:XML in xmlList)
				{
					goodsIDs.push((int)(xml.toXMLString()));
				}
				dispatch(new GetGoodsByTypeIDEvent(GetGoodsByTypeIDEvent.GET_GOODS_BY_TYPEID, goodsIDs));
			}
		}
	}
}